package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author huangxin
 * @description
 * @date 2020/12/3
 */
@RestController
@RequestMapping("/goodsType")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    // 查询商品所有分类
    // http://localhost:8080/goodsType/loadGoodsType
    @PostMapping("/loadGoodsType")
    public String getGoodsCategory() {
        // 从数据库查询出所有分类信息
        List<JSONObject> rootCategory = goodsTypeService.getGoodsCategory();
        return JSON.toJSONString(rootCategory);
    }

    // 新增分类
    // http://localhost:8080/goodsType/save
    @PostMapping("/save")
    @SuppressWarnings("rawtypes")
    public ServiceVO addCategory(String goodsTypeName, Integer pId) {
        goodsTypeService.addCategory(goodsTypeName, pId);
        return new ServiceVO(100, "请求成功");
    }

    // 删除分类
    // http://localhost:8080/goodsType/delete
    @PostMapping("/delete")
    @SuppressWarnings("rawtypes")
    public ServiceVO deleteCategory(Integer goodsTypeId) {
        goodsTypeService.deleteCategory(goodsTypeId);
        return new ServiceVO(100, "请求成功");
    }
}
